/*
	File:		PowerUp.cpp
	Course:		SGD 1301
	Author:		Peter Nguyen
	Purpose:	CPowerUp class handles the powerup tiles
*/
#include "PowerUp.h"
#include "Ship.h"
#include "Game.h"
#include "..\SGD Wrappers\CSGD_TextureManager.h"
#include <cassert>
CPowerUp::CPowerUp(int _intType) 
{
	if(_intType == 1)
		m_eType = ENT_POWERUP1;
	else if(_intType == 2)
		m_eType = ENT_POWERUP2;
	else if(_intType == 3)
		m_eType = ENT_POWERUP3;
	else if(_intType == 4)
		m_eType = ENT_POWERUP4;
	else
		assert(false && "CPowerUp::CPowerUp(int _intType) : PARAM OUTTA RANGE!!!");
	respawnTime = 0;
	spawned = true;
}


CPowerUp::~CPowerUp()
{
	CTiles::~CTiles();
}
void CPowerUp::Update( float fElapsedTime )
{
	if(!spawned)
	{
		respawnTime -= fElapsedTime;
		if(respawnTime<=0)
			spawned = true;
	}
}
void CPowerUp::Render( void )
{
	if(spawned)
	{
		CGame* gamePtr = CGame::GetInstance();

		POINT start = {(int)GetPosX()*GetWidth()-gamePtr->GetCameraX(),(int)GetPosY()*GetHeight()-gamePtr->GetCameraY()};
		rotate_point(gamePtr->GetWidth()/2.0f,gamePtr->GetHeight()/2.0f,gamePtr->GetCameraRotation(),start);

		RECT srcRECT = {0,0,512,512};
		float scale = 512.0f / GetWidth();

		CSGD_TextureManager::GetInstance()->Draw( GetImageID(),
			start.x, start.y,
			1.0f/scale,1.0f/scale,&srcRECT, 
			GetWidth() *0.5f,GetHeight()*0.5f,gamePtr->GetCameraRotation());
	}
}
bool CPowerUp::CheckCollision( IEntity* pOther )
{
	if(spawned)
	{
		if((pOther->GetType()==CEntity::ENT_INPUTSHIP)||(pOther->GetType()==CEntity::ENT_AISHIP))
		{
			CShip* pShip = dynamic_cast<CShip*>(pOther);
			float wH = m_nWidth/2.0f;
			float hH = m_nWidth/2.0f;
			float xCen = m_fPosX * m_nWidth  + wH;
			float yCen = m_fPosY * m_nHeight + hH;
			float shipX = pShip->GetPosX();
			float shipY = pShip->GetPosY();
			float shipWH = pShip->GetWidth()/2.0f;
			float shipHH = pShip->GetHeight()/2.0f;

			if(xCen+wH<shipX-shipWH)
				return false;
			if(xCen-wH>shipX+shipWH)
				return false;
			if(yCen+hH<shipY-shipHH)
				return false;
			if(yCen-hH>shipY+shipHH)
				return false;
			//////////////////////
			//Collided, now apply powerup.
			if(GetType()==EntityType::ENT_POWERUP1)
			{
				pShip->SetSpeed(2000);
			
			if(pShip->GetType()==ENT_INPUTSHIP)
				CGame::GetInstance()->ChangeScore(1337);
			}
			else if(GetType()==EntityType::ENT_POWERUP2)
			{
				pShip->spinOutTimer= 1.0f;
				pShip->spinOutVector.fX = rand()%(40-20)/10.0f;
				pShip->spinOutVector.fY = rand()%(40-20)/10.0f;
			}
			else if(GetType()==EntityType::ENT_POWERUP3)
			{
				pShip->SetUnstunableTimer(3.0f);
			if(pShip->GetType()==ENT_INPUTSHIP)
				CGame::GetInstance()->ChangeScore(1337);
			}
			else if(GetType()==EntityType::ENT_POWERUP4)
			{
				if(pShip->GetType()==ENT_INPUTSHIP)
					CGame::GetInstance()->ChangeScore(1337);
				pShip->SetFastFireTimer(4.0f);
			}
			respawnTime = 4.0f;
			spawned = false;
			return true;
		}
	}
	return false;
}